package cn.itheima.web;

import cn.itheima.domain.SalaryTable;
import cn.itheima.domain.Section;
import cn.itheima.domain.User;
import cn.itheima.service.UserService;
import cn.itheima.service.UserServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author: cuisensen
 * data: 2018/7/5.
 * time: 21:25.
 */
@WebServlet("/showSalaryTable")
public class ShowSalaryTableServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uid=null!=request.getParameter("uid")?request.getParameter("uid"):"";
        UserService us=new UserServiceImp();
        User user = us.findUserById(uid);
        SalaryTable st=new SalaryTable();
        st.setBase(user.getSalary());
        if (user.getRoot()<3){
            st.setManager(Math.random()*1000);
        }else {
            st.setManager(0.0);
        }
        st.setSection(Math.random()*1000);
        st.setRevenue((st.getBase()+st.getManager()+st.getSection())*st.getPercent());
        st.setEnd((st.getBase()+st.getManager()+st.getSection())*(1-st.getPercent()));
        request.setAttribute("salaryTable",st);
        request.setAttribute("user",user);
        request.getSession().setAttribute("includePage","salaryTable.jsp");
        request.getRequestDispatcher(request.getContextPath()+"/index.jsp").forward(request,response);
    }
}
